/**
 *Public funtions 
 */
function pingNetWork(params) {


    var url = params.url;
    if (url == '') {
        api.toast({ msg: '请求url为空！' });
        return;
    }

    var sizeCmd = params.sizeCmd;
    if (sizeCmd == '') {
        sizeCmd = '32';
    }

    var timeCmd = params.timeCmd;
    if (timeCmd == '') {
        timeCmd = '0.5';
    }

    var timeOut = params.timeOut;
    if (timeOut == '') {
        timeOut = '5';
    }

    var modulePing = api.require('modulePing');

    modulePing.pingtest({
        ip: url,
        sizeCmd: sizeCmd,
        timeCmd: timeCmd,   //时间隔
        timeOut: timeOut,   //超时时间
    }, function (ret, err) {
        if (ret) {
            api.hideProgress();
            if (ret.okNum > 0) {
                api.toast({
                    msg: '服务器异常'
                });
            } else {
                api.toast({
                    msg: '网络不通'
                });
            }
        } else {
            api.hideProgress();
            alert(JSON.stringify(err));
        }
    });
}

function ajaxForData(params, data, isshow, callback) {
    var oldVer = api.appVersion;
    data.values.appVersion = oldVer;

    var url = params.url;
    if (url == '') {
        api.toast({ msg: '请求url为空！' });
        return;
    }

    var method = params.method;
    if (method == '') {
        api.toast({ msg: '请求method为空！' });
        return;
    }

    var dataType = params.dataType;
    if (dataType == '') {
        api.toast({ msg: '请求dataType为空！' });
        return;
    }

    var encode = params.encode;
    if (encode == '') {
        encode = true;
    }

    var tag = params.tag;
    if (tag == '') {
        tag = 'normal';
    }

    // var timeout = params.timeout;
    // if (timeout == '') {
    //     timeout = 300;
    // }

    var timeout = 300;

    var cache = params.cache;
    if (cache == '') {
        cache = false;
    }

    var charset = params.charset;
    if (charset == '') {
        charset = 'utf-8';
    }

    var report = params.report;
    if (report == '') {
        report = false;
    }

    var returnAll = params.returnAll;
    if (returnAll == '') {
        returnAll = false;
    }

    if (isshow) {
        api.showProgress();
    }
    var tenant_id = localStorage.getItem('tenantId');
    api.ajax({
        url: url,
        encode: encode,
        tag: tag,
        method: method,
        cache: cache,
        timeout: timeout,
        dataType: dataType,
        ContentType: "application/json",
        headers: { 'tenant_id': tenant_id },
        charset: charset,
        report: report,
        returnAll: returnAll,
        data: data
    }, function (ret, err) {
        api.hideProgress();
        // if (ret == '') {
        //     var params = new Object();
        //     params.url = localStorage.getItem('serverIpN');
        //     pingNetWork(params);
        //     return;
        // }

        var retObj = ret;
        var rettype = typeof (ret);

        if (rettype == 'string') {
            console.log(rettype.type);
            retObj = eval("(" + ret + ")");
        }
        //强制更新APP
        if (retObj.msg == "APP强制更新") {
            // console.log(retObj.ver);
            var ver = retObj.ver;
            checkUpdate(ver);
        } else  {
            if (callback) {
                // console.log(retObj);
                callback(retObj, err);
            }
        } 
        // else {
        //     api.toast({
        //         msg: ret.msg,
        //         duration: 3000
        //     });
        // }
    });
}

//强制更新
function checkUpdate(ver) {
    var serverIp = "";
    var serverPort = "";
    var serverProject = "";
    //判断是内网还是外网配置
    var netChoice = localStorage.getItem('netChoice');
    if (netChoice == "out") {
        //外网访问
        serverIp = localStorage.getItem('serverIpW');
        serverPort = localStorage.getItem('serverPortW');;
        serverProject = localStorage.getItem('serverProjectW');
    } else {
        //内网访问
        serverIp = localStorage.getItem('serverIpN');
        serverPort = localStorage.getItem('serverPortN');;
        serverProject = localStorage.getItem('serverProjectN');
    }
    var msg = "版本号：" + ver;
    api.confirm({
        title: 'APP版本需要升级，是否强制更新APP?',
        msg: msg,
        buttons: ['确定', '取消']
    }, function (ret, err) {
        var index = ret.buttonIndex;
        if (index == 1) {
            var downloadUrl = "http://" + serverIp + ":" + serverPort + "/download/" + "HKAPP_" + ver + ".apk";//HKAPP_
            api.download({
                url: downloadUrl,
                report: true,
                savePath: 'fs://' + 'HKAPP_' + ver + '.apk',
            }, function (ret, err) {
                if (ret && 0 == ret.state) { /* 下载进度 */
                    api.toast({
                        msg: "正在下载应用" + ret.percent + "%",
                        duration: 10000
                    });
                }

                if (ret && 1 == ret.state) { /* 下载完成 */
                    var savePath = ret.savePath;
                    // alert(ret.savePath);
                    api.installApp({
                        appUri: savePath
                    });
                }

                if (ret && 2 == ret.state) {
                    api.toast({
                        msg: "下载失败",
                        duration: 10000
                    });
                }
            });
        } else {
            api.closeWidget({
                silent: true
            });
        }
    });
}

function getNowDate() {
    var date = new Date;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    var time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    return time;
}

function getTimeCommon() {
    var date = new Date;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    var time = year + "年" + month + "月" + day + "日 " + hours + ":" + minutes + ":" + seconds;
    return time;
}
